<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="学院/部门" prop="xyId">
        <el-select v-model="queryParams.xyId" placeholder="请选择">
          <el-option
            v-for="item in proxyList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教工号" prop="jsNumber">
        <el-input
          v-model="queryParams.jsNumber"
          placeholder="请输入教工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:prouserter:add']"
          style="font-size: 14px; color: blue;"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:prouserter:edit']"
          style="font-size: 14px; color: #3ac27c;"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:prouserter:remove']"
          style="font-size: 14px; color: #F56C6C;"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:prouserter:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prouserterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="教师ID" align="center" prop="userId" />
      <el-table-column label="uniId" align="center" prop="uniId" />
      <el-table-column label="openID" align="center" prop="openId" />
      <el-table-column label="openId2" align="center" prop="openId2" /> -->
      <el-table-column label="头像" align="center" prop="userImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.userImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" min-width="150px"/>
      <el-table-column label="教工号" align="center" prop="jsNumber" min-width="150px"/>
      <!-- <el-table-column label="密码" align="center" prop="password" /> -->
      <!-- <el-table-column label="学校" align="center" prop="xxName" /> -->
      <el-table-column label="学院/部门" align="center" prop="xyName" min-width="200px"/>
      <!-- <el-table-column label="职位" align="center" prop="zwName" min-width="300px" :show-overflow-tooltip="true"/> -->
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="380px">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAuth(scope.row)"
            v-hasPermi="['system:user:edit']"
          >权限管理</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePassword(scope.row)"
            v-hasPermi="['project:prouser:edit']"
            style="font-size: 14px; color: #3ac27c;"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:prouserter:edit']"
            style="font-size: 14px; color: #3ac27c;"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleDc(scope.row)"-->
<!--            v-hasPermi="['system:user:edit']"-->
<!--          >督促</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:prouserter:remove']"
            style="font-size: 14px; color: #F56C6C;"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px"  style="width: 90%; min-height: 35vh">
        <!-- <el-form-item label="uniId" prop="uniId">
          <el-input v-model="form.uniId" placeholder="请输入uniId" />
        </el-form-item>
        <el-form-item label="openID" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入openID" />
        </el-form-item>
        <el-form-item label="openId2" prop="openId2">
          <el-input v-model="form.openId2" placeholder="请输入openId2" />
        </el-form-item> -->
        <!-- <el-form-item label="学校" prop="xxId">
          <el-select v-model="form.xxId" placeholder="请选择">
            <el-option
              v-for="item in proxxList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="学院/部门" prop="xyId">
            <el-select v-model="form.xyId" placeholder="请选择">
            <el-option
              v-for="item in proxyList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="职位" prop="zwId">
            <el-select v-model="form.zwId" placeholder="请选择">
            <el-option
              v-for="item in prozwList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions"  placeholder="请选择归属部门" />
        </el-form-item> -->
        <!-- <el-form-item label="角色">
            <el-select v-model="form.roleIds" multiple placeholder="请选择角色">
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              ></el-option>
            </el-select>
        </el-form-item> -->


        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="头像" prop="userImage">
          <image-upload v-model="form.userImage" :limit='1'/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="教工号" prop="jsNumber">
          <el-input-number v-model="form.jsNumber" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

        <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitresetPassword">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openAuth" width="400px" append-to-body>
      <el-form ref="formAuth" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="权限列表" prop="authList">
              <el-tree
                :data="treexxList"
                default-expand-all
                :default-checked-keys="form.authListSelect"
                @check-change="handleCheckChange"
                ref="treeAuth"
                :show-checkbox="true"
                node-key="key"
                :props="authProps">
              </el-tree>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAuth">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProuserter, getProuserter, delProuserter, addProuserter, updateProuserter,resetPassword,listProxy,listProzy,listPronj,listProbj,listProzw,listProxx,deptTreeSelect,listXxTree,subAuth,listUserAuth} from "@/api/project/prouserter";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Prouserter",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师用户表格数据
      prouserterList: [],
      //
      prozyList:[],
      proxyList:[],
      proxxList:[],
      pronjList:[],
      probjList:[],
      prozwList:[],
      //学校树形
      treexxList:[],
      // 部门树选项
      deptOptions: undefined,
      // 角色选项
      roleOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      openAuth: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uniId: null,
        openId: null,
        openId2: null,
        name: null,
        userImage: null,
        phone: null,
        jsNumber: null,
        password: null,
        xyId: null,
        xxId: null,
        zwId: null,
        status: null
      },
      // 表单参数
      form: {
        authList: [],
        authListSelect :[]
      },
      authProps: {
        children: 'childList',
        label: 'name',
        value:'id',
        key: 'key'
      },
      // 表单校验
      rules: {
         xyId: [
          { required: true, message: "学院不能为空", trigger: "blur" }
        ],
        jsNumber: [
          { required: true, message: "教工号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
  this.getInit();
    this.getList();
  },
  methods: {
    /** 查询教师用户列表 */
    getList() {
      this.loading = true;
      listProuserter(this.queryParams).then(response => {
        this.prouserterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getInit(){
      // this.getDeptTree();
      listXxTree().then(response => {
          this.treexxList = response.data;
        });
      listProxy().then(response => {
        this.proxyList = response.rows;
      });
      listProzy().then(response => {
        this.prozyList = response.rows;
      });
      listPronj().then(response => {
        this.pronjList = response.rows;
      });
      listProbj().then(response => {
        this.probjList = response.rows;
      });
      listProzw().then(response => {
        this.prozwList = response.rows;
      });
      listProxx().then(response => {
        this.proxxList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.openAuth = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        uniId: null,
        openId: null,
        openId2: null,
        name: null,
        userImage: null,
        phone: null,
        jsNumber: null,
        password: null,
        createTime: null,
        xyId: null,
        xxId: null,
        zwId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增教师信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getProuserter(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑教师信息";
      });
    },
    /** 重置密码 */
    handlePassword(row) {
      // this.reset();
      // const userId = row.userId || this.ids
      // getProuserter(userId).then(response => {
      //   this.form.userId = response.data.userId;
      //   this.open2 = true;
      //   this.title = "修改密码";
      // });
        this.$confirm('是否重置密码？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.form.userId = row.userId || this.ids
        resetPassword({}).then(res => {
          if(res.code == 200){
            this.$message.success('重置成功')
            this.open2 = false;
            this.getList();
          }
      });
      }).catch(() => {});
    },
        /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        console.log(response.data,'部门');
      });
    },
    submitresetPassword() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            resetPassword(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateProuserter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProuserter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除教师用户编号为"' + userIds + '"的数据项？').then(function() {
        return delProuserter(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/prouserter/export', {
        ...this.queryParams
      }, `prouserter_${new Date().getTime()}.xlsx`)
    },
    handleCheckChange () {

      // // 目前被选中的菜单节点
      // let checkedNodes = this.$refs.treeAuth.getCheckedNodes();
      // // 半选中的菜单节点
      // let checkedNodes = this.$refs.treeAuth.getCheckedNodes();
      // checkedNodes.unshift.apply(checkedNodes, checkedNodes);
      // this.form.authList = checkedKeys;

        const checkedNodes = this.$refs.treeAuth.getCheckedNodes()   //  利用这个方法就可以获取到子节点
        const AllcheckedNodes = this.$refs.treeAuth.getCheckedNodes(false, true); // 利用这个方法就可以获取到子节点+父节点
        checkedNodes.unshift.apply(checkedNodes, AllcheckedNodes);
        // console.log(checkedNodes,'1');
        // console.log(AllcheckedNodes,'2');

        this.form.authList = AllcheckedNodes;
    },
     /** 用户权限查看 */
    handleAuth(row) {
      this.reset();
      this.form.authListSelect = [];
      if (this.$refs.treeAuth != undefined) {
        this.$refs.treeAuth.setCheckedNodes([]);
      }
      const userId = row.userId || this.ids;
      var map = {};
      map.userId = userId;
      console.log(map);
      listUserAuth(map).then(response => {
        this.form = response.data;
        // console.log(response.data);
        console.log(this.form);
        this.openAuth = true;
        this.title = "分配权限";
        this.form.password = "";
      });
    },
        /** 提交权限 */
    submitFormAuth: function() {
      this.$refs["formAuth"].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            subAuth(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAuth = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDc(){
      this.$modal.msgSuccess("督促成功");
    }
  }

};
</script>
